package org.edu.modules.stu.base.service.impl;

import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.modules.stu.base.dto.VeBaseStudentDto;
import org.edu.modules.stu.base.entity.VeStuXjAudit;
import org.edu.modules.stu.base.mapper.VeStuChangeMapper;
import org.edu.modules.stu.base.entity.VeStuChange;
import org.edu.modules.stu.base.dto.VeStuChangeDto;
import org.edu.modules.stu.base.mapper.VeStuXjAuditMapper;
import org.edu.modules.stu.base.service.VeBaseStudentService;
import org.edu.modules.stu.base.service.VeStuChangeService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.stu.common.entity.VeBaseBanji;
import org.edu.modules.stu.common.entity.VeBaseFaculty;
import org.edu.modules.stu.common.entity.VeBaseSpecialty;
import org.edu.modules.stu.common.mapper.VeBaseBanjiMapper;
import org.edu.modules.stu.common.mapper.VeBaseFacultyMapper;
import org.edu.modules.stu.common.mapper.VeBaseSpecialtyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 实现类
 */
@Service
public class VeStuChangeServiceImpl extends BaseServiceImpl<VeStuChange, VeStuChangeMapper> implements VeStuChangeService {

    @Autowired
    private VeBaseStudentService veBaseStudentService;

    @Autowired
    private VeBaseFacultyMapper veBaseFacultyMapper;

    @Autowired
    private VeBaseSpecialtyMapper veBaseSpecialtyMapper;

    @Autowired
    private VeBaseBanjiMapper veBaseBanjiMapper;

    @Autowired
    private VeStuXjAuditMapper veStuXjAuditMapper;

    @Autowired
    private VeStuChangeMapper veStuChangeMapper;

    public IPage<VeStuChangeDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper) {
        return mapper.pageDto(page, queryWrapper);
    }

    /**
     * 查询实体类
     */
    public VeStuChangeDto findDtoById(String id) {
        VeStuChange entity = super.findById(id);
        VeStuChangeDto dto = ConvertUtils.convertToDto(entity, VeStuChangeDto.class);
        return dto;
    }

    /**
     * 新增或者修改
     */
    @Transactional
    public void insertOrUpdate(VeStuChangeDto dto, Boolean isAdd) {
        if (isAdd) {
            //根据学生id查这个学生
            VeBaseStudentDto baseStudentDto = veBaseStudentService.findDtoById(dto.getStuId().toString());
            if (baseStudentDto != null) {
                dto.setCode(baseStudentDto.getXh());
                dto.setName(baseStudentDto.getXm());
                dto.setOfalId(baseStudentDto.getFalId());
                dto.setOfalText(baseStudentDto.getFalText());
                dto.setOspecId(baseStudentDto.getSpecId());
                dto.setOspecText(baseStudentDto.getSpecText());
                dto.setOgradeId(baseStudentDto.getGradeId());
                dto.setOgradeTest(baseStudentDto.getGradeText());
                dto.setObjId(baseStudentDto.getBjId());
                dto.setObjText(baseStudentDto.getBjText());
                if (dto.getYdlbm().equals("1")) {
                    //退学
                    baseStudentDto.setXsdqztm("TX");
                } else if (dto.getYdlbm().equals("2")) {
                    //休学
                    baseStudentDto.setXsdqztm("XX");
                } else if (dto.getYdlbm().equals("3")) {
                    //转专业,学院，专业，年级，班级id都有传
                    VeBaseFaculty veBaseFaculty = veBaseFacultyMapper.selectById(dto.getFalId());
                    if (veBaseFaculty != null) {
                        dto.setFalText(veBaseFaculty.getYxmc());
                        baseStudentDto.setFalId(dto.getFalId());
                        baseStudentDto.setFalText(veBaseFaculty.getYxmc());
                    }
                    VeBaseSpecialty veBaseSpecialty = veBaseSpecialtyMapper.selectById(dto.getSpecId());
                    if (veBaseSpecialty != null) {
                        dto.setSpecText(veBaseSpecialty.getZymc());
                        baseStudentDto.setSpecId(dto.getSpecId());
                        baseStudentDto.setSpecText(veBaseSpecialty.getZymc());
                    }
                    VeBaseBanji baseBanji = veBaseBanjiMapper.selectById(dto.getBjId());
                    if (baseBanji != null) {
                        dto.setBjText(baseBanji.getXzbmc());
                        dto.setGradeText(baseBanji.getNjmc());
                        baseStudentDto.setGradeId(dto.getGradeId());
                        baseStudentDto.setGradeText(baseBanji.getNjmc());
                        baseStudentDto.setBjId(dto.getBjId());
                        baseStudentDto.setBjText(baseBanji.getXzbmc());
                    }
                } else if (dto.getYdlbm().equals("4")) {
                    //留级 传年级，班级id  学院和专业不变
                    VeBaseBanji baseBanji = veBaseBanjiMapper.selectById(dto.getBjId());
                    if (baseBanji != null) {
                        dto.setBjText(baseBanji.getXzbmc());
                        dto.setGradeText(baseBanji.getNjmc());
                        baseStudentDto.setGradeId(dto.getGradeId());
                        baseStudentDto.setGradeText(baseBanji.getNjmc());
                        baseStudentDto.setBjId(dto.getBjId());
                        baseStudentDto.setBjText(baseBanji.getXzbmc());
                    }
                    dto.setFalId(baseStudentDto.getFalId());
                    dto.setFalText(baseStudentDto.getFalText());
                    dto.setSpecId(baseStudentDto.getSpecId());
                    dto.setSpecText(baseStudentDto.getSpecText());
                } else if (dto.getYdlbm().equals("6")) {
                    //转出
                    baseStudentDto.setXsdqztm("ZXX");
                }

            }
            super.insert(dto);
            //修改学生表字段is_change 是否学籍异动(0:未异动，1:异动)
            baseStudentDto.setIsChange(1);
            veBaseStudentService.update(baseStudentDto);
            //插入学生学籍审核表 暂时不审核
            VeStuXjAudit veStuXjAudit = new VeStuXjAudit();
            veStuXjAudit.setStuId(baseStudentDto.getId());
            veStuXjAudit.setInfo("");
            veStuXjAudit.setTerminalId(0);
            veStuXjAuditMapper.insert(veStuXjAudit);
        } else {
            //异动恢复，学生学院，专业，年级，班级信息改回原来
            VeStuChange veStuChange = veStuChangeMapper.selectById(dto.getId());
            if (veStuChange != null) {
                //根据学生id查这个学生
                VeBaseStudentDto baseStudentDto = veBaseStudentService.findDtoById(veStuChange.getStuId().toString());
                if (baseStudentDto != null) {
                    //异动恢复到原来
                    baseStudentDto.setFalId(veStuChange.getOfalId());
                    baseStudentDto.setFalText(veStuChange.getOfalText());
                    baseStudentDto.setSpecId(veStuChange.getOspecId());
                    baseStudentDto.setSpecText(veStuChange.getOspecText());
                    baseStudentDto.setGradeId(veStuChange.getOgradeId());
                    baseStudentDto.setGradeText(veStuChange.getOgradeTest());
                    baseStudentDto.setBjId(veStuChange.getObjId());
                    baseStudentDto.setBjText(veStuChange.getObjText());
                    //修改学生表字段is_change 是否学籍异动(0:未异动，1:异动),改回未异动
                    baseStudentDto.setIsChange(0);
                    veBaseStudentService.update(baseStudentDto);
                }
            }
            //LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            dto.setIsRecover(1);
            dto.setRecoverTime(new Date());
            super.update(dto);
        }
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList) {
        for (Map map : mapList) {
            VeStuChangeDto dto = new VeStuChangeDto();
            //         dto.setCode ((String)map.get(0000));
            //         dto.setName ((String)map.get(0000));
            //         dto.setStuId ((String)map.get(0000));
            //         dto.setYdlbm ((String)map.get(0000));
            //         dto.setYdDate ((String)map.get(0000));
            //         dto.setYdMsg ((String)map.get(0000));
            //         dto.setSpDate ((String)map.get(0000));
            //         dto.setSpwh ((String)map.get(0000));
            //         dto.setJbrgh ((String)map.get(0000));
            //         dto.setYdlyxxm ((String)map.get(0000));
            //         dto.setYdqxxm ((String)map.get(0000));
            //         dto.setYdsm ((String)map.get(0000));
            //         dto.setOfalId ((String)map.get(0000));
            //         dto.setOspecId ((String)map.get(0000));
            //         dto.setObjId ((String)map.get(0000));
            //         dto.setOgradeId ((String)map.get(0000));
            //         dto.setFalId ((String)map.get(0000));
            //         dto.setSpecId ((String)map.get(0000));
            //         dto.setBjId ((String)map.get(0000));
            //         dto.setGradeId ((String)map.get(0000));
            //         dto.setIsRecover ((String)map.get(0000));
            //         dto.setRecoverUserId ((String)map.get(0000));
            //         dto.setRecoverTime ((String)map.get(0000));

            super.insert(dto);
        }

    }

}
